package com.yanqu.road.server.cmd.activity.drink;

import com.yanqu.road.entity.activity.drink.DrinkBlock;
import com.yanqu.road.entity.activity.drink.DrinkEliminateTips;
import com.yanqu.road.entity.activity.drink.DrinkUserData;
import com.yanqu.road.entity.activity.drink.config.DrinkConfig;
import com.yanqu.road.pb.activity.DrinkActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.drink.DrinkModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.drink.DrinkEliminateLogic;
import com.yanqu.road.server.manger.activity.drink.DrinkMgr;
import com.yanqu.road.server.pb.DrinkPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_DRINK_GET_ELIMINATE_TIPS, desc = "获取消除提示")
public class DrinkGetEliminateTipsCmd extends AbstractCommand {

    private int multi;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DrinkActivityProto.DrinkGetEliminateTipsReqMsg reqMsg = DrinkActivityProto.DrinkGetEliminateTipsReqMsg.parseFrom(packet.getBytes());
        if (reqMsg.hasNewMulti()) {
            multi = reqMsg.getNewMulti();
        } else {
            multi = reqMsg.getMulti() ? 8 : 1;
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DrinkConfig drinkConfig = DrinkMgr.getDrinkConfig();
        if (drinkConfig == null || !ActivityMgr.activityInTime(drinkConfig.getActivityInfo())) {
            ret = GameErrorCode.E_DRINK_ACTIVITY_NO_OPEN;
            return;
        }

        // 旧配置的活动不能用16倍
        if (multi == 16 && drinkConfig.getFastUnlockVipLevel() == Integer.MAX_VALUE) {
            ret = GameErrorCode.E_DRINK_ACTIVITY_NO_UNLOCK_MULTI;
            return;
        }

        DrinkModule module = player.getModule(DrinkModule.class);
        DrinkUserData userData = module.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_DRINK_ACTIVITY_NO_UNLOCK;
            return;
        }

        long now = System.currentTimeMillis();
        if (now - userData.getLastTipsTime() < 2500) {
            // 防止频繁请求，客户端间隔3秒
            return;
        }
        userData.setLastTipsTime(now);

        DrinkEliminateTips tips = DrinkEliminateLogic.calDrinkEliminateTips(userData.getBlockList(multi));
        if (tips.isSuccess()) {
            DrinkActivityProto.DrinkGetEliminateTipsRespMsg.Builder builder = DrinkActivityProto.DrinkGetEliminateTipsRespMsg.newBuilder();
            builder.setRet(0);
            builder.setFromBlock(DrinkPb.parseDrinkBlockMsg(tips.getFrom()));
            if (tips.getTo() != null) {
                builder.setToBlock(DrinkPb.parseDrinkBlockMsg(tips.getTo()));
            }
            for (DrinkBlock drinkBlock : tips.getBlockList()) {
                builder.addBlockList(DrinkPb.parseDrinkBlockMsg(drinkBlock));
            }
            player.sendPacket(ClientProtocol.U_DRINK_GET_ELIMINATE_TIPS, builder);
        } else {
            log.error("DrinkMgr calDrinkEliminateTips false on DrinkGetEliminateTipsCmd! activityId={}, userId={}, rearrange!", userData.getActivityId(), userData.getUserId());
            int[][] copyBlockList = DrinkMgr.getCopyBlockList(userData.getBlockList(multi));
            DrinkEliminateLogic.rearrange(copyBlockList);
            userData.setBlockList(copyBlockList, multi);
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
